import { Request, Response, NextFunction } from 'express';
import * as attributesService from '../models/attributesService';
import { json } from 'stream/consumers';

// 创建自定义标签 
export const addRibutes = async (req: Request, res: Response) => {
    const { user_id, family_id, attributes_name } = req.body;
    // 检查参数是否完整
    if (!user_id || !family_id || !attributes_name) {
        res.status(400).json({
            message: "请提供完整的数据"
        });
        return
    }
    try {
        const result = await attributesService.addRibute(user_id, family_id, attributes_name);
        if (typeof result === 'string') {
            // 如果服务层返回的是（字符串类型）
            res.status(400).json({
                message: result
            });
            return
        } else {
            res.status(200).json({
                message: "创建成功"
            });
            return
        }
    } catch (error) {
        res.status(500).json({
            message: "服务器错误，创建标签失败"
        });
    }
};

// 获取所有自定义标签
export const getTagsByUsersAndFamily = async (req: Request, res: Response) => {
    const { user_id, family_id } = req.query;
    // 检查参数是否完整
    if (!family_id) {
        res.status(400).json({
            message: "请提供完整的数据"
        });
        return
    }
    try {
        const result = await attributesService.getTagsByUserAndFamily(Number(user_id), String(family_id));
        res.status(200).json({
            data: result
        });
    } catch (error) {
        res.status(500).json({
            message: "服务器错误，获取标签列表失败"
        });
    }
};

// 删除自定义标签  
export const deleteRibutes = async (req: Request, res: Response) => {
    const { attributes_id, user_id, family_id } = req.body
    console.log("req.body", req.body)
    if (!attributes_id || !family_id) {
        res.status(400).json({
            message: "请提供完整的数据"
        });
        return
    } else {
        const result = await attributesService.deleteRibute(attributes_id, user_id, family_id)
        if (typeof result === 'string') {
            res.status(400).json({
                message: result
            });
            return
        } else {
            res.status(200).json({
                message: "删除成功"
            });
            return
        }
    }
}